<?php
//教练信息管理
namespace Admin\Controller;

class CoachController extends ComController{
    //教练列表
    public function index(){
        
        $d_id = session('uid'); //驾校id
        $coach = D('coach');//实例化教练模型
        $car = D('car');//实例化车辆模型
        $license_type = D('license_type');//实例化驾照模型
        $p = I('get.p');
        //分页
        $p = intval($p) > 0 ? $p : 1;
        $pagesize = 5;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量

        $where = 'c.d_id = '.$d_id.' and c.status != 2';
        $count_where = 'd_id = '.$d_id.' and status != 2';
        $name = I('get.name') ? I('get.name','','strip_tags') : '';
        $driver_type = I('get.driver_type') ? I('get.driver_type') : '';
        
        if (!empty($driver_type)) {
            $where .= " and c.driver_type = ".$driver_type;
            $count_where .= " and driver_type = " .$driver_type;
        }
        if ($name) {
            $where .= " and c.name like '%".trim($name)."%' ";
            $count_where .= " and name like '%".trim($name)."%' ";
        }
        //默认按照时间降序
        $orderby = "c.add_time desc";
        
        $count = $coach->where($count_where)->count();
        $coach_data = $coach->alias(' as c')->field(' c.id ,c.name,c.sex,c.driver_age,c.phone,c.status,c.img,c.car_num,l.type,m.name as sc_name,c.d_id')->join('inner join driving_license_type as l  on c.driver_type = l.id')->join(' inner join driving_member as m on c.d_id = m.uid')->where($where)->order($orderby)->limit($offset . ',' . $pagesize)->select();
        $this->assign('coach',$coach_data);

        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        //驾照类型
        $l_data = $license_type->where(array('d_id'=>$d_id))->select();
        $this->assign('license_type',$l_data);
        $this->assign('page', $page);
        $this->display();
    }

    //添加教练信息
    public function append(){
        $d_id = session('uid');//驾校id
        if(IS_POST){
            // $name = I('post.name') ? I('post.name') : '';
            // $age = I('post.age') ? I('post.age') : '';
            // $sex = I('post.sex') ? I('post.sex') : '';
            // $phone = I('post.phone') ? I('post.phone') : '';
            // $driver_age = I('post.driver_age') ? I('post.driver_age') : '';
            // $head = I('post.head') ? I('post.head') : '';
            // $l_type = I('post.l_type') ? I('post.l_type') : '';
            // $car_id = I('post.car_id') ? I('post.car_id') : '';
            // $intro = I('post.intro') ? I('post.intro') : '';
            // if(!$name) $this->error('请填写教练姓名!');
            // if(!$age) $this->error('请填写教练年龄!');
            // if(!$phone) $this->error('请填写教练电话号码!');
            // if(!$driver_age) $this->error('请填写教练驾龄!');
            // if(!$l_type) $this->error('请选择驾照类别!');
            // if(!$car_id) $this->error('请为教练分配车辆!');
            // if(!$intro) $this->error('请填写教练简介!');
            $coach = D('coach');
            $param = $coach->create(I('post'));
            if($param){
                $param['d_id'] = $d_id; //添加驾校id
                //实例化车辆模型
                $car = D('car');
                $car_data = $car->where(array('id'=>$param['car_id'],'d_id'=>$d_id))->find();
                if($car_data) $param['car_num'] = $car_data['car_num'];
                $param['add_time'] = time();
                $res = $coach->add($param);
                if($res){
                    //分配车辆后改变车辆管理表中字段状态
                    $car = D('car');
                    $car_data = $car->where(array('id'=>$param['car_id'],'d_id'=>$d_id))->save(array('status'=>1));
                    $this->success('添加教练信息成功！',U('index'));die;
                }else{
                    $this->error('添加教练信息失败：'.$coach->getError());die;
                }
            }else{
               $this->error($coach->getError());
            }
        }else{
            //获取驾照类型
            $license_type = M('license_type');
            $l_data = $license_type->where('d_id = '.$d_id)->select();
            $this->assign('license_type',$l_data);
            //获取车辆信息
            $car = D('car');
            $car_data = $car->where('status = 0 and d_id = '.$d_id)->select();
            $this->assign('car',$car_data);
            $this->display();
        }
    }


    //修改教练信息
    public function edit(){
        $d_id = session('uid');//驾校id

        if(IS_POST){
            $coach = D('coach');
            $car = M('car');
            $param = I('post.');
            $id = $param['id'];
            unset($param['id']);
            $coach_data = $coach->where(array('id'=>$id))->find();
            if($param && $coach_data){
                if (isset($param['car_id']) && $param['car_id'] != $coach_data['car_id']) {
                        $param['car_num'] = $car->where(array('id'=>$param['car_id']))->find()['car_num'];
                         $car->where(array('id'=>$coach_data['car_id']))->save(array('status'=>0));
                         $car->where(array('id'=>$param['car_id']))->save(array('status'=>1));

                }
                $res = $coach->where(array('id'=>$id))->save($param);
                if($res === false){
                    $this->error('修改教练信息失败'.$coach->getError());die;
                }else{
                    $this->success('修改教练信息成功！',U('index'));die;
                }
            }else{
                $this->error('修改教练信息失败'.$coach->getError());die;
            }
        }else{
            $id = I('get.id') ? I('get.id') : 0;//修改教练的主键id
            //获取驾照类型
            $license_type = M('license_type');
            $l_data = $license_type->where('d_id = '.$d_id)->select();
            $this->assign('license_type',$l_data);
            //获取车辆信息
            $car = D('car');
            $car_data = $car->where('(status = 0 or status = 1)and d_id = '.$d_id)->select();
            $this->assign('car',$car_data);
            //获取教练信息
            $coach = D('coach');
            $coach_data = $coach->where('d_id = '.$d_id.' and id = '.$id)->find();
            $this->assign('coach',$coach_data);
            $this->display();
        }
    }

    //删除教练信息
    public function del(){
        $id = I('get.id') ? I('get.id') : 0;
        $d_id = session('uid'); //驾校id
        $where['id'] = $id;
        $where['d_id'] = $d_id;
        $coach = D('coach');
        $coach_data = $coach->where($where)->find();
        $appointment = D('appointment_coach');
        $orderby = 'add_time desc';
        $appointment_data = $appointment->where(array('school_id'=>$d_id,'coach_id'=>$id))->order($orderby)->find();
        if($appointment_data){

             $this->error('教练排班信息已发布，不能删除!');
             die;
        }
        if($coach_data){
            //删除图片
            $img = empty($coach_data['img']) ? '' : $coach_data['img'];
                
            if($img && file_exists(ROOT.$img)){
                @unlink(ROOT.$img);
            }
            //更改车辆状态
            $car_id = $coach_data['car_id'];
            //删除排班列表
            
            $car = D('car');
            $car_data = $car->where(array('id'=>$car_id))->save(array('status'=>0));
            $res = $coach->where($where)->save(array('status'=>2));
            if($res){
                $this->success('删除教练信息，成功!',U('index'));
            }else{
                $this->error('删除教练信息，失败!'.$coach->getError());
            }
        }else{
            $this->error('没有找到记录，信息删除失败!',U('index'));
        }
    }

    /**
     * ajax更改教练状态
     */
    public function setStatus(){
        $status_id = I('status_id') ? I('status_id') : '';
        if($status === ''){
            echo json_encode(array('res'=>0,'data'=>'','msg'=>'失败'));die;
        }else{
            $status_id = explode('-', $status_id);
            $status = $status_id[0];
            $id = $status_id[1];
            $coach = D('coach');
            $res = $coach->where(array('id'=>$id))->save(array('status'=>$status));
            if($res){
              echo   json_encode(array('res'=>1,'data'=>'','msg'=>'成功'));die;
            }else{
              echo   json_encode(array('res'=>0,'data'=>'','msg'=>'失败'));die;
            }
        }
    }


}
?>